package com.bfly.core.base.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.enums.SysError;
import com.bfly.core.security.Login;
import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/9/17 14:00
 */
@RestController
public class IndexController {

    @GetMapping(value = "")
    public void index(HttpServletResponse response){
        ResponseUtil.writeJson(response, ResponseData.getSuccess("API服务"));
    }

    /**
     * 登录提示
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/17 14:01
     */
    @GetMapping(value = "/login")
    @Login(required = false)
    public void loginTip(HttpServletResponse response) {
        ResponseUtil.writeJson(response, ResponseData.getFail(SysError.UNAUTHORIZED, "未登录!"));
    }

    /**
     * 验证码生成
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/17 10:24
     */
    @GetMapping(value = "/captcha.svl")
    @Login(required = false)
    public void generateCaptcha(HttpServletResponse response, HttpServletRequest request) throws Exception {
        byte[] captchaChallengeAsJpeg;
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        ImageCaptchaService captchaService = BeanFactoryUtils.beanOfTypeIncludingAncestors(appCtx, ImageCaptchaService.class);
        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        try {
            String captchaId = request.getSession().getId();
            BufferedImage challenge = captchaService.getImageChallengeForID(captchaId, request.getLocale());
            ImageIO.write(challenge, "png", jpegOutputStream);
        } catch (IllegalArgumentException e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        } catch (CaptchaServiceException e) {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }

        captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/png");

        ServletOutputStream responseOutputStream = response.getOutputStream();
        responseOutputStream.write(captchaChallengeAsJpeg);
        responseOutputStream.flush();
        responseOutputStream.close();
    }
}
